﻿using System;
using System.Web;
using System.Collections.Specialized;

namespace Evpo.ContactData.UI
{
    public class UrlHelper
    {
        #region Constants
        public const string HomePageName = "Default.aspx";
        #endregion Constants

        #region Fields
        #endregion

        #region Properties
        #endregion

        #region Methods
        public static Uri SetQueryParam(Uri baseUri, string paramName, string value)
        {
            
            NameValueCollection collection = HttpUtility.ParseQueryString(baseUri.Query);
            collection[paramName] = value;

            return new UriBuilder(baseUri)
            {
                Query = collection.ToString()
            }.Uri;
        }

        public static Uri ContactUrl(int contactID, Uri currentUri)
        {
            return ContactUrl(contactID, currentUri, typeof(ContactSummary).Name);
        }

        public static Uri ContactUrl(int contactID, Uri currentUri, string formName)
        {
            return UrlWithID("ID", contactID, currentUri, formName);
        }

        public static Uri UrlWithID(string idParameterName, int id, Uri currentUri, string formName)
        {
            NameValueCollection collection = HttpUtility.ParseQueryString(String.Empty);
            collection[idParameterName] = id.ToString();

            Uri newUrl = new Uri(currentUri, formName + ".aspx");
            var uriBuilder = new UriBuilder(newUrl);
            

            uriBuilder.Query = collection.ToString();
            return uriBuilder.Uri;
        }

        public static Uri AddressUrl(int addressID, Uri currentUri, int? contactID)
        {
            Uri retVal = UrlWithID("AddressID", addressID, currentUri, typeof(AddressEditForm).Name);
            if (contactID.HasValue)
                retVal = SetQueryParam(retVal, "ContactID", contactID.Value.ToString());
            return retVal;
        }

        #endregion Methods
    }

}
